<template>
  <div class="prompts-container">
    <h1>角色设置</h1>
    
    <div class="prompts-list">
      <div v-for="prompt in prompts" :key="prompt.id" class="prompt-item">
        <div class="prompt-info">
          <div class="prompt-header">
            <div class="prompt-icon">{{ getRandomRoleEmoji() }}</div>
            <h3>{{ prompt.name }}</h3>
            <span v-if="prompt.isDefault" class="default-badge">默认</span>
          </div>
          <p class="prompt-content">{{ prompt.content }}</p>
        </div>
        <div class="prompt-actions">
          <button @click="editPrompt(prompt)" class="edit">
            <span class="icon">✏️</span>
            编辑
          </button>
          <button 
            v-if="!prompt.isDefault" 
            @click="handleSetDefault(prompt)"
            class="default"
          >
            <span class="icon">⭐️</span>
            设为默认
          </button>
          <button 
            v-if="prompts.length > 1" 
            @click="handleDelete(prompt.id)" 
            class="delete"
          >
            <span class="icon">🗑️</span>
            删除
          </button>
        </div>
      </div>
    </div>

    <button @click="showAddModal = true" class="add-btn">
      <span class="icon">➕</span>
      添加角色
    </button>

    <div v-if="showAddModal" class="modal">
      <div class="modal-content">
        <h2>{{ editingPrompt ? '编辑角色' : '添加角色' }}</h2>
        <form @submit.prevent="handleSubmit">
          <div class="form-item">
            <label>角色名称：</label>
            <input v-model="formData.name" required placeholder="如：专业翻译助手" />
          </div>
          <div class="form-item">
            <label>角色设定：</label>
            <textarea 
              v-model="formData.content" 
              required 
              rows="5"
              placeholder="输入角色的系统提示词..."
            ></textarea>
            <div class="help-text">描述这个角色的特点、专长和行为方式</div>
          </div>
          <div class="form-item checkbox">
            <label>
              <input type="checkbox" v-model="formData.isDefault">
              设为默认角色
            </label>
          </div>
          <div class="form-actions">
            <button type="button" @click="showAddModal = false" class="cancel">取消</button>
            <button type="submit" class="submit">确定</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import type { SystemPrompt } from '@/types/model'
import { getPrompts, createPrompt, deletePrompt, setDefaultPrompt } from '@/utils/request'
import { ElMessage } from 'element-plus'

const prompts = ref<SystemPrompt[]>([])
const showAddModal = ref(false)
const editingPrompt = ref<SystemPrompt | null>(null)
const formData = ref({
  name: '',
  content: '',
  isDefault: false
})

// 添加随机角色表情
const roleEmojis = ['🤖', '👨‍🏫', '👨‍💻', '🎨', '🎭', '📚', '🔬', '🌍', '🎯', '💡']
const getRandomRoleEmoji = () => {
  return roleEmojis[Math.floor(Math.random() * roleEmojis.length)]
}

// 获取提示词列表
const getPromptsList = async () => {
  try {
    const res = await getPrompts()
    if (res.data.status === 'success') {
      prompts.value = res.data.data
    }
  } catch (error) {
    console.error('获取提示词列表失败:', error)
  }
}

const resetForm = () => {
  formData.value = {
    name: '',
    content: '',
    isDefault: false
  }
}

// 编辑提示词
const editPrompt = (prompt: SystemPrompt) => {
  editingPrompt.value = prompt
  formData.value = {
    name: prompt.name,
    content: prompt.content,
    isDefault: prompt.isDefault || false
  }
  showAddModal.value = true
}

// 设置默认提示词
const handleSetDefault = async (prompt: SystemPrompt) => {
  try {
    await setDefaultPrompt(prompt.id)
    // 重新获取列表
    await getPromptsList()
  } catch (error) {
    console.error('设置默认失败:', error)
  }
}

// 删除提示词
const handleDelete = async (id: number) => {
  if (confirm('确定要删除这个角色吗？')) {
    try {
      await deletePrompt(id)
      // 重新获取列表
      await getPromptsList()
    } catch (error) {
      console.error('删除失败:', error)
    }
  }
}

// 提交表单
const handleSubmit = async () => {
  try {
    const res = await createPrompt(formData.value)
    if (res.data.status === 'success') {
      ElMessage.success('创建成功')
      showAddModal.value = false
      await getPromptsList()
      resetForm()
    }
  } catch (error) {
    ElMessage.error('创建失败')
  }
}

// @ts-ignore
const handleUpdate = async () => {
  // ... 函数内容
}
</script>

<style scoped>
.prompts-container {
  padding: 2rem;
}

h1 {
  color: #1f2937;
  margin-bottom: 2rem;
}

.prompts-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.prompt-item {
  background: white;
  padding: 1.5rem;
  border: 1px solid #e5e7eb;
  border-radius: 0.75rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.prompt-header {
  display: flex;
  align-items: center;
  margin-bottom: 0.75rem;
}

.prompt-icon {
  font-size: 1.5rem;
  margin-right: 0.75rem;
}

.prompt-header h3 {
  margin: 0;
  color: #1f2937;
  font-size: 1.1rem;
}

.default-badge {
  background: #10b981;
  color: white;
  padding: 0.2rem 0.75rem;
  border-radius: 1rem;
  font-size: 0.8rem;
  font-weight: 500;
}

.prompt-content {
  color: #4b5563;
  white-space: pre-wrap;
  margin: 0.75rem 0;
  line-height: 1.5;
  font-size: 0.95rem;
}

.prompt-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 1rem;
}

button {
  padding: 0.5rem 1rem;
  border-radius: 0.5rem;
  cursor: pointer;
  border: none;
  font-weight: 500;
  transition: all 0.2s;
}

.edit {
  background: #f3f4f6;
  color: #374151;
}

.edit:hover {
  background: #e5e7eb;
}

.default {
  background: #10b981;
  color: white;
}

.default:hover {
  background: #059669;
}

.delete {
  background: #fee2e2;
  color: #dc2626;
}

.delete:hover {
  background: #fecaca;
}

.add-btn {
  margin-top: 2rem;
  background: #2563eb;
  color: white;
  padding: 0.75rem 1.5rem;
  font-size: 1rem;
}

.add-btn:hover {
  background: #1d4ed8;
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  padding: 2rem;
  border-radius: 0.75rem;
  width: 90%;
  max-width: 500px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.modal-content h2 {
  color: #1f2937;
  margin-bottom: 1.5rem;
}

.form-item {
  margin-bottom: 1.5rem;
}

.form-item label {
  display: block;
  margin-bottom: 0.5rem;
  color: #374151;
  font-weight: 500;
}

.form-item input,
.form-item textarea {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #e5e7eb;
  border-radius: 0.5rem;
  font-size: 0.95rem;
}

.form-item textarea {
  resize: vertical;
  min-height: 100px;
}

.form-item.checkbox {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.form-item.checkbox label {
  margin: 0;
  cursor: pointer;
}

.form-item.checkbox input {
  width: auto;
  cursor: pointer;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 1rem;
  margin-top: 2rem;
}

.form-actions button {
  padding: 0.75rem 1.5rem;
  font-size: 0.95rem;
}

.cancel {
  background: #f3f4f6;
  color: #374151;
}

.cancel:hover {
  background: #e5e7eb;
}

.submit {
  background: #2563eb;
  color: white;
}

.submit:hover {
  background: #1d4ed8;
}

input:focus,
textarea:focus {
  outline: none;
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}
</style> 